package org.bambrikii.lang.translation.translationmodule;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.bambrikii.lang.translation.translationmodule.model.PartOfSpeech;
import org.bambrikii.lang.translation.translationmodule.model.Phrase;
import org.bambrikii.lang.translation.translationmodule.model.Translation;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;

public class GoogleTranslator implements Translator {
	public String translate1(final String phrase, final String fromLanguage,
			final String toLanguage) throws ClientProtocolException,
			IOException {
		final HttpClient client = new DefaultHttpClient();
		final HttpGet request = new HttpGet(
				"http://translate.google.com/translate_a/t?client=t&text="
						+ phrase
						+ "&hl=en&sl=auto&tl=en&multires=1&prev=btn&ssel=0&tsel=0&uptl=en"
						 + "&alttl=en&sc=1");
		// request.setHeader("Accept-Charset", "utf-8");

		final HttpResponse response = client.execute(request);
		final HttpEntity entity = response.getEntity();
		final StringWriter writer = new StringWriter();
		IOUtils.copy(entity.getContent(), writer,//
				// "windows-1251"
				"ISO-8859-1" //
				// "US-ASCII"//
				// "utf-8"
				);
		client.getConnectionManager().shutdown();
		return StringUtils.U2U(writer.toString());
	}

	public Translation translate(final String phrase, final String sourceLang,
			final String targetLang) throws TranslationException {
		try {
			final String str = translate1(phrase, sourceLang, targetLang);
			final JsonParser p = new JsonParser();
			final JsonElement value = p.parse(str);
			final Translation tr = new Translation();
			final JsonArray result1 = value.getAsJsonArray();
			tr.setPhrase(new Phrase() {
				{
					final JsonArray phrase2 = result1.get(0).getAsJsonArray()
							.get(0).getAsJsonArray();
					setSource(phrase2.get(1).getAsJsonPrimitive().getAsString());
					setTranscription(phrase2.get(2).getAsJsonPrimitive()
							.getAsString());
					setTranslation(phrase2.get(0).getAsJsonPrimitive()
							.getAsString());
				}
			});
			tr.setSourceLanguage(result1.get(2).getAsJsonPrimitive()
					.getAsString());
			tr.setPartsOfSpeech(new ArrayList<PartOfSpeech>() {
				private static final long serialVersionUID = 5784783402679620591L;
				{
					final JsonArray phrases1 = result1.get(1).getAsJsonArray();
					for (int i = 0; i < phrases1.size(); i++) {
						final JsonArray phrase3 = phrases1.get(i)
								.getAsJsonArray();
						add(new PartOfSpeech() {
							{
								setType(phrase3.get(0).getAsJsonPrimitive()
										.getAsString());
								setVariations(new ArrayList<String>() {
									private static final long serialVersionUID = 4149793097639052803L;
									{
										final JsonArray phrases4 = phrase3.get(
												1).getAsJsonArray();
										for (int j = 0; j < phrases4.size(); j++) {
											final JsonPrimitive phrase5 = phrases4
													.get(j)
													.getAsJsonPrimitive();
											add(phrase5.getAsString());
										}
									}
								});
							}
						});
					}
				}
			});
			return tr;
		} catch (final ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (final IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		throw new TranslationException();
	}
}
